/**
 * http配置
 */
import axios from 'axios'
import router from './router/index'
import { Message } from 'element-ui';

//http request请求拦截器，有token值则配置上token值
axios.interceptors.request.use(
  config => {
    if (config.url.indexOf('./static') == 0) {
      return config;
    }

    if (config.url.indexOf('/loginProductController/login') == 0) {
      return config;
    }

    config.headers.authorization = "";
    if (window.localStorage.getItem("token")) { // 每次发送请求之前判断是否存在token，如果存在，则统一在http请求的header都加上token，不用每次请求都手动添加了
      config.headers.authorization = window.localStorage.getItem("token");
    }
    return config;
  },
  err => {
    return Promise.reject(err);
});

//http response服务器响应拦截器，这里拦截401错误，并重新跳入登录界面重新获取token值
axios.interceptors.response.use(
  response => {
     if(response.config.url == "./static/data.json"){
       return response.data;
     }
     //封装文件流进字段
     var outFileArr = [
       '/userController/userExportRecordExport',
       '/alarmRecord/alarmRecordExport',
       '/deviceCommonInfoController/deviceExportRecordExport'
     ]

     var reqUrl = response.config.url;
     if(response.config.method == "get"){
        reqUrl = reqUrl.split("?")[0];
     }

     //报表类过滤器
    for (let i = 0; i < outFileArr.length; i++) {
      const ele = outFileArr[i];
      if (reqUrl == outFileArr[i]) {
        var resNew = {
          message: '您没有权限做此操作！',
          status: 600,
          file: response.data
        }
        if (response.headers.status === '600') {
          Message.error(resNew.message)
          return resNew;
        } else {
          resNew.message = '导出成功';
          resNew.status = 200;
          return resNew;
        }
      }
    }
     //操作类过滤器
     if(response.data.status == 600){//无权操作
            //无权操作
            var operataArr = [
              '/menuInfoController/findMenuList',
              '/menuInfoController/queryFatherMenu',
              '/menuInfoController/addMenuInfo',
              '/menuInfoController/delMenuInfo',
              '/menuInfoController/updateMenInfo',
              '/menuInfoController//queryMenuChecked',
              '/menuInfoController/menusIndexse',
              '/menuInfoController/queryMenuTree',
              '/menuInfoController/queryOperateList',
              '/roleInfoController/findList',
              '/roleInfoController/findRoleList',
              '/roleInfoController/addRoleInfo',
              '/roleInfoController/delRoleInfo',
              '/roleInfoController/updateRoleInfo',
              '/roleInfoController/assign',
              '/userController/findList',
              '/userController/addUserInfo',
              '/userController/delUser',
              '/userController/updateUser',
              '/userController/delUserList',
              '/userController/updatePasswordByUId',
              '/userController/querySubordinateUser',
              '/emailController/addEmail',
              '/emailController/findEmail',
              '/userProDevicesController/queryUserDevice',
              '/userProDevicesController/bindDevice',
              '/deviceTypeController/queryAllDeviceType',
              '/deviceCommonInfoController/devicesPage',
              '/deviceCommonInfoController/queryDeviceInfo',
              '/deviceCommonInfoController/updateDeviceInfoAttribute',
              '/deviceImportInfoController/addDevices',
              '/deviceCommonInfoController/deviceInfoConfiguration',
              '/deviceCommonInfoController/deviceCount',
              '/deviceCommonInfoController/queryNewDevice',
              '/deviceCommonInfoController/queryDeviceAP',
              '/deviceCommonInfoController/queryOnLineDeviceAP',
              '/deviceCommonInfoController/queryAbnormalDeviceAp',
              '/userController/userExportRecordExport',
              '/deviceCommonInfoController/queryRtmpUrl',
              '/deviceCommonInfoController/deviceExportRecordExport',
              '/alarmRecord/abnormalBar',
              '/alarmRecord/search',
              '/alarmRecord/updateRemark',
              '/alarmRecord/emailNotification',
              '/alarmRecord/shortNoteNotification',
              '/alarmRecord/updateStatus',
              '/alarmRecord/abnormalStatistics',
              '/alarmRecord/alarmRecordExport'
           ]
           operataArr.forEach((val, idx) => {
             if (reqUrl == val) {
               Message.error(response.data.message);
            }
        });
     } else if(response.data.status == 601){//登录过期
        Message.error(response.data.message);
        window.localStorage.removeItem("token");//这里写清除token的代码
        router.replace({
          path:'/login'
       })
     }
    return response.data;
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          window.localStorage.removeItem("token");//这里写清除token的代码
          router.replace({
            path: '/login',
            query: {
              redirect: router.currentRoute.fullPath
            },//登录成功后跳入浏览当前页面
          })
      }
    }
    return Promise.reject(error.response);
  });

export default axios;
